import copy
import json
import os

from commons import case_func_dict
from commons.common import dict_value_2_getkey
from files import gloabal_dict
from cases import main_cases

# case_func_dict
# dict_value_2_getkey

# 用例加载后，才有
class FileJsonLimitArgs:
    flag = True
    file_ini_path = './datafiles/main_limit_args.json'
    file_ini_dir = './datafiles'
    args_dict_list:list[dict] = []
    args_key_list = ['id', 'title', 'limit_L', 'limit_H', 'match_method', 'enable', 'loop', 'fresh_loop', 'force_loop', 'pre_time','post_time','fail_time', 'modify_flag', 'modify', 'case_func', 'func_input_args']
    @classmethod
    def init(cls):
        # Logger.logger.info('ini文件初始化。。。')
        try:
            if not os.path.exists(cls.file_ini_dir):
                os.mkdir(cls.file_ini_dir)
                cls.flag = False
            if not os.path.exists(cls.file_ini_path):
                with open(cls.file_ini_path, 'w'):  # 创建文件
                    pass
                # Path(file_ini_dir).touch()
                cls.flag = False
            # 创建文件初始内容
            if not cls.flag:
                print('asdasdasd',main_cases)
                num = 1
                for main_case in main_cases:
                    # print(main_case.__dict__)
                    # m = copy.copy(main_case.__dict__)
                    ID:str = 'main_'+ str(num).rjust(2,'0')
                    args_value_list = main_case.get_save_args()

                    args_dict = {}
                    func_info = []      # 每个用例有单独的函数参数信息
                    for key,value in zip(cls.args_key_list, (ID, *args_value_list)):
                        if key == 'case_func':      # 对于函数需要这样处理
                            args_dict[key] = dict_value_2_getkey(case_func_dict, value)
                            continue
                        args_dict[key] = value
                    # print(args_dict)
                    num += 1
                    cls.args_dict_list.append(args_dict)

                with open(cls.file_ini_path, 'w', encoding='utf-8') as f:
                    json.dump(cls.args_dict_list, f, ensure_ascii=False, indent=4)

        except Exception as e:
            # Logger.logger.error('ini文件初始化失败')
            print('limit_args加载失败')
            pass

    @classmethod
    def read_json_global(cls):
        # 从JSON文件中读取数据
        with open(cls.file_ini_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        return data